//
//  OrderInfoCell.m
//  danpin
//
//  Created by chuangjia on 27/10/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "OrderInfoCell.h"
#import "TTTAttributedLabel.h"
#import "OrderInfo.h"
#import "CalcLabel.h"
@interface OrderInfoCell ()
{
    
}
@property(nonatomic,strong)UILabel *sectionLabel;
@property(nonatomic,strong)UIButton *helpbtn;
@property(nonatomic,strong)UIView *lineview;
//
@property(nonatomic, strong) TTTAttributedLabel* title;
@property(nonatomic,strong) UIImageView *titleIcon;
@property(nonatomic,strong) UILabel * lbQty;
@property(nonatomic,strong) UILabel * lbSizeColor;
@property(nonatomic,strong)UIView *line2view;
//
@property(nonatomic,strong)UILabel *ddyfkey;
@property(nonatomic,strong)UILabel *ddyfvalue;

@property(nonatomic,strong)UILabel *gjyfkey;
@property(nonatomic,strong)UILabel *gjyfvalue;
@property(nonatomic,strong)UIButton *gjhelpbtn;

@property(nonatomic,strong)UILabel *ysyfkey;
@property(nonatomic,strong)UILabel *ysyfvalue;

@property(nonatomic,strong)UILabel *hbzhyfkey;
@property(nonatomic,strong)UILabel *hbzhyfvalue;

@property(nonatomic,strong)UILabel *hjspkey;
@property(nonatomic,strong)UILabel *hjspvalue;
@property(nonatomic,strong)UIButton *hjspBtn;

@end
@implementation OrderInfoCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
        
        _sectionLabel =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2,20)];
        _sectionLabel.backgroundColor = [UIColor clearColor];
        _sectionLabel.textColor = CONTEXT_COLOR;
        _sectionLabel.textAlignment = NSTextAlignmentLeft;
        _sectionLabel.font = FONT(12);
        [self.contentView addSubview:_sectionLabel];
        //告诉用户总价的计算方式，各个细项的收费原因
        _helpbtn =[UIButton buttonWithType:UIButtonTypeCustom];
        _helpbtn.backgroundColor = [UIColor clearColor];
        [_helpbtn setImage:[UIImage imageNamed:@"help.png"] forState:UIControlStateNormal];
        _helpbtn.tag=1;
        _helpbtn.frame =CGRectMake(PHOTO_FRAME_WIDTH,(40-16)/2, 16, 16);
        [_helpbtn addTarget:self action:@selector(clickHelpEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_helpbtn];
        
        _lineview =[[UIView alloc] initWithFrame:CGRectZero];
        _lineview.backgroundColor=UIColorFromRGB(0xe5e5e5);
        [self.contentView addSubview:_lineview];
        
        //
        _title =[[TTTAttributedLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH*8,PHOTO_FRAME_WIDTH, DEVICE_WIDTH-PHOTO_FRAME_WIDTH* 10,40)];
        _title.backgroundColor = [UIColor clearColor];
        _title.textColor = [UIColor darkTextColor];//UIColorFromRGB(0x808080);
        _title.textAlignment = NSTextAlignmentLeft;
        _title.lineBreakMode = NSLineBreakByWordWrapping|NSLineBreakByTruncatingTail;
        _title.numberOfLines=0;
        _title.font = FONT(13);
        [self.contentView addSubview:_title];
        
        _titleIcon =[[UIImageView alloc] initWithFrame:CGRectZero];
        _titleIcon.frame =CGRectMake(PHOTO_FRAME_WIDTH,PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH*6, PHOTO_FRAME_WIDTH*6);
        _titleIcon.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_titleIcon];
        
        
        _lbSizeColor =[[UILabel alloc] initWithFrame:CGRectZero];
        _lbSizeColor.backgroundColor = [UIColor clearColor];
        _lbSizeColor.textColor =  [UIColor darkGrayColor];
        _lbSizeColor.lineBreakMode = NSLineBreakByTruncatingTail;
        _lbSizeColor.textAlignment = NSTextAlignmentLeft;
        _lbSizeColor.font =  FONT(11);
        _lbSizeColor.numberOfLines=0;
        _lbSizeColor.frame =CGRectMake(PHOTO_FRAME_WIDTH*8, PHOTO_FRAME_WIDTH*5.5, DEVICE_WIDTH - PHOTO_FRAME_WIDTH*10, PHOTO_FRAME_WIDTH*2);
        [self.contentView addSubview:_lbSizeColor];
        
        _lbQty =[[UILabel alloc] initWithFrame:CGRectZero];
        _lbQty.backgroundColor = [UIColor clearColor];
        _lbQty.textColor =  [UIColor blackColor];
        _lbQty.font =  FONT_BOLD(12);
        _lbQty.numberOfLines=0;
        _lbQty.textAlignment = NSTextAlignmentRight;
        _lbQty.frame =CGRectMake(DEVICE_WIDTH - 120, PHOTO_FRAME_WIDTH*5.5, 110, PHOTO_FRAME_WIDTH*2);
        
        [self.contentView addSubview:_lbQty];
        
        _line2view =[[UIView alloc] initWithFrame:CGRectZero];
        _line2view.backgroundColor=UIColorFromRGB(0xe5e5e5);
        [self.contentView addSubview:_line2view];
        //
        _ddyfkey =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2,20)];
        _ddyfkey.backgroundColor = [UIColor clearColor];
        _ddyfkey.textColor = [UIColor darkGrayColor];
        _ddyfkey.textAlignment = NSTextAlignmentLeft;
        _ddyfkey.font = FONT(12);
        [self.contentView addSubview:_ddyfkey];
        
        _ddyfvalue =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH,20)];
        _ddyfvalue.backgroundColor = [UIColor clearColor];
        _ddyfvalue.textColor = [UIColor blackColor];
        _ddyfvalue.textAlignment = NSTextAlignmentRight;
        _ddyfvalue.font = FONT(12);
        [self.contentView addSubview:_ddyfvalue];
        
        //
        _gjyfkey =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2,20)];
        _gjyfkey.backgroundColor = [UIColor clearColor];
        _gjyfkey.textColor = [UIColor darkGrayColor];
        _gjyfkey.textAlignment = NSTextAlignmentLeft;
        _gjyfkey.font = FONT(12);
        [self.contentView addSubview:_gjyfkey];
        
        _gjyfvalue =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH,20)];
        _gjyfvalue.backgroundColor = [UIColor clearColor];
        _gjyfvalue.textColor = [UIColor blackColor];
        _gjyfvalue.textAlignment = NSTextAlignmentRight;
        _gjyfvalue.font = FONT(12);
        [self.contentView addSubview:_gjyfvalue];
        //告诉用户总价的计算方式，各个细项的收费原因
        _gjhelpbtn =[UIButton buttonWithType:UIButtonTypeCustom];
        _gjhelpbtn.backgroundColor = [UIColor clearColor];
        [_gjhelpbtn setImage:[UIImage imageNamed:@"help.png"] forState:UIControlStateNormal];
        _gjhelpbtn.tag=4;
        _gjhelpbtn.frame =CGRectMake(PHOTO_FRAME_WIDTH,(40-16)/2, 16, 16);
        [_gjhelpbtn addTarget:self action:@selector(clickHelpEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_gjhelpbtn];
        //
        _ysyfkey =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2,20)];
        _ysyfkey.backgroundColor = [UIColor clearColor];
        _ysyfkey.textColor = [UIColor darkGrayColor];
        _ysyfkey.textAlignment = NSTextAlignmentLeft;
        _ysyfkey.font = FONT(12);
        [self.contentView addSubview:_ysyfkey];
        
        _ysyfvalue =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH,20)];
        _ysyfvalue.backgroundColor = [UIColor clearColor];
        _ysyfvalue.textColor = [UIColor blackColor];
        _ysyfvalue.textAlignment = NSTextAlignmentRight;
        _ysyfvalue.font = FONT(12);
        [self.contentView addSubview:_ysyfvalue];
        
        //
        _hbzhyfkey =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2,20)];
        _hbzhyfkey.backgroundColor = [UIColor clearColor];
        _hbzhyfkey.textColor =[UIColor darkGrayColor];
        _hbzhyfkey.textAlignment = NSTextAlignmentLeft;
        _hbzhyfkey.font = FONT(12);
        [self.contentView addSubview:_hbzhyfkey];
        
        _hbzhyfvalue =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH,20)];
        _hbzhyfvalue.backgroundColor = [UIColor clearColor];
        _hbzhyfvalue.textColor = [UIColor blackColor];
        _hbzhyfvalue.textAlignment = NSTextAlignmentRight;
        _hbzhyfvalue.font = FONT(12);
        [self.contentView addSubview:_hbzhyfvalue];
        //
        _hjspkey =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2,20)];
        _hjspkey.backgroundColor = [UIColor clearColor];
        _hjspkey.textColor = [UIColor darkGrayColor];
        _hjspkey.textAlignment = NSTextAlignmentLeft;
        _hjspkey.font = FONT(12);
        [self.contentView addSubview:_hjspkey];
        
        _hjspvalue =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH,20)];
        _hjspvalue.backgroundColor = [UIColor clearColor];
        _hjspvalue.textColor = RED_COLOR;
        _hjspvalue.textAlignment = NSTextAlignmentRight;
        _hjspvalue.font = FONT(15);
        [self.contentView addSubview:_hjspvalue];
        
        //告诉用户总价的计算方式，各个细项的收费原因
        _hjspBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        _hjspBtn.tag = 2;
        _hjspBtn.backgroundColor = [UIColor clearColor];
        [_hjspBtn setImage:[UIImage imageNamed:@"help.png"] forState:UIControlStateNormal];
        _hjspBtn.frame =CGRectMake(PHOTO_FRAME_WIDTH,(40-16)/2, 16, 16);
        [_hjspBtn addTarget:self action:@selector(clickHelpEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_hjspBtn];
        
        
    }
    return self;
}
-(void)dealloc
{
    self.actionBlock =nil;
    self.data=nil;
    self.indexPath=nil;
    self.other=nil;
    self.key=nil;
    
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    /*
     dimsInfo = "Cherry Blossom Flower Graphic";
     image = "http://ecx.images-amazon.com/images/I/41NyhgP8vwL._SY400_.jpg";
     price = 54;
     qty = 1;
     shipFeeLocal = 0;
     shipFeeOverseas = 40;
     siteName = "amazon_us";
     taxPrice = 0;
     title = "Contigo AUTOSPOUT Kids Striker Water Bottle, 14-Ounce, Cherry Blossom Flower Graphic";
     totalPrice = "71.38500000000001";
     weightKg = "0.363";
     weightUnit = "0.8\U78c5";
     conversionFee
     */
    self.key =key;
    self.indexPath=indexPath;
    NSDictionary *dict =(NSDictionary*)model;
    
    self.data =model;
    //
    _sectionLabel.text =[dict stringForKey:@"siteName" withDefaultValue:@""];
    _sectionLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH, 0,DEVICE_WIDTH, 40);
    
    _helpbtn.frame =CGRectMake(DEVICE_WIDTH-26,(40-16)/2, 16, 16);
    _lineview.frame =CGRectMake(0, 39.5,DEVICE_WIDTH, 0.5);
    //
    
    NSString *urlstr =[dict stringForKey:@"image" withDefaultValue:@""];
    if (urlstr.length>0) {
        _titleIcon.frame =CGRectMake(PHOTO_FRAME_WIDTH,PHOTO_FRAME_WIDTH*5, PHOTO_FRAME_WIDTH*6, PHOTO_FRAME_WIDTH*6);
        [_titleIcon sd_setImageWithURL:[NSURL URLWithString:urlstr]];
    }
    float fheight = 0;
    NSString *title =[dict stringForKey:@"title" withDefaultValue:@""];
    if (title.length>0) {
        [self.title setText:title afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString  *(NSMutableAttributedString *attributedString)
         {
             [attributedString addAttribute:NSFontAttributeName
                                      value:FONT(12)
                                      range:NSMakeRange(0,[attributedString length])];
             //设置字体颜色
             [attributedString addAttribute:(id)kCTForegroundColorAttributeName value:TITLE_COLOR range:NSMakeRange(0,[attributedString length])];
       
             return attributedString;
             
         }];
        
        fheight = [CalcLabel getAttributedStringHeightWidthValue:(DEVICE_WIDTH- PHOTO_FRAME_WIDTH*10) str:self.title.attributedText];
        
        if (fheight>40) {
            fheight=40;
        }
        
        self.title.frame = CGRectMake(PHOTO_FRAME_WIDTH*8, PHOTO_FRAME_WIDTH*4.5,DEVICE_WIDTH- PHOTO_FRAME_WIDTH*10, fheight);
        
        self.lbSizeColor.frame = CGRectMake(PHOTO_FRAME_WIDTH*8, self.title.frame.origin.y + fheight ,DEVICE_WIDTH- PHOTO_FRAME_WIDTH*11, 20);
        self.lbSizeColor.text =[dict stringForKey:@"dimsInfo" withDefaultValue:@""];
        
    }
   
    self.lbQty.frame =CGRectMake(DEVICE_WIDTH - 120, 120 - PHOTO_FRAME_WIDTH*2, 110, PHOTO_FRAME_WIDTH*2);
    NSString *price =[dict stringForKey:@"price" withDefaultValue:@""];
    NSString *qty =[dict stringForKey:@"qty" withDefaultValue:@""];
    if (price.length>0 && qty.length>0) {
        self.lbQty.text = [NSString stringWithFormat:@"¥ %@ x %@", price,qty];
    }
    
    _line2view.frame =CGRectMake(0, 119.5,DEVICE_WIDTH, 0.5);
    
    //
    _ddyfkey.frame =CGRectMake(PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH*13,DEVICE_WIDTH/2, 20);
    _ddyfvalue.frame =CGRectMake(DEVICE_WIDTH - DEVICE_WIDTH/2 -PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH*13,DEVICE_WIDTH/2, 20);
    _ddyfkey.text = @"当地运费: ";
    NSString *shipFeeLocal =[dict stringForKey:@"shipFeeLocal" withDefaultValue:@""];
    if (shipFeeLocal.length>0) {
        _ddyfvalue.text =[NSString stringWithFormat:@"¥ %@", shipFeeLocal];
    }
    
    //
    _gjyfkey.frame =CGRectMake(PHOTO_FRAME_WIDTH, _ddyfkey.frame.origin.y+_ddyfkey.frame.size.height+PHOTO_TIME_PADDING,DEVICE_WIDTH/2, 20);
    _gjyfvalue.frame =CGRectMake(DEVICE_WIDTH - DEVICE_WIDTH/2 -PHOTO_FRAME_WIDTH, _ddyfkey.frame.origin.y+_ddyfkey.frame.size.height+PHOTO_TIME_PADDING,DEVICE_WIDTH/2, 20);
    NSString *weightKg =[dict stringForKey:@"weightKg" withDefaultValue:@""];
    if (weightKg.length>0) {
         _gjyfkey.text =  [NSString stringWithFormat:@"国际运费 (%@, 多退少补): ", weightKg];
    }
    _gjhelpbtn.frame =CGRectMake(170,_gjyfkey.frame.origin.y+3, 16, 16);
    NSString *shipFeeOverseas =[dict stringForKey:@"shipFeeOverseas" withDefaultValue:@""];
    if (shipFeeOverseas.length>0) {
        _gjyfvalue.text = [NSString stringWithFormat:@"¥ %@", shipFeeOverseas];
    }
    
    //
    _ysyfkey.frame =CGRectMake(PHOTO_FRAME_WIDTH, _gjyfkey.frame.origin.y+_gjyfkey.frame.size.height+PHOTO_TIME_PADDING,DEVICE_WIDTH/2, 20);
    _ysyfvalue.frame =CGRectMake(DEVICE_WIDTH - DEVICE_WIDTH/2 -PHOTO_FRAME_WIDTH, _gjyfkey.frame.origin.y+_gjyfkey.frame.size.height+PHOTO_TIME_PADDING,DEVICE_WIDTH/2, 20);
    _ysyfkey.text = @"预收关税 (多退少补): ";
    NSString *taxPrice =[dict stringForKey:@"taxPrice" withDefaultValue:@""];
    if (taxPrice.length>0) {
        _ysyfvalue.text =[NSString stringWithFormat:@"¥ %@", taxPrice];
    }
    
    //
    _hbzhyfkey.frame =CGRectMake(PHOTO_FRAME_WIDTH, _ysyfkey.frame.origin.y+_ysyfkey.frame.size.height+PHOTO_TIME_PADDING,DEVICE_WIDTH/2, 20);
    _hbzhyfvalue.frame =CGRectMake(DEVICE_WIDTH - DEVICE_WIDTH/2 -PHOTO_FRAME_WIDTH, _ysyfkey.frame.origin.y+_ysyfkey.frame.size.height+PHOTO_TIME_PADDING,DEVICE_WIDTH/2, 20);
    _hbzhyfkey.text = @"货币转换费: ";
    NSString *conversionFee =[dict stringForKey:@"conversionFee" withDefaultValue:@""];
    if (conversionFee.length>0) {
        _hbzhyfvalue.text =[NSString stringWithFormat:@"¥ %@", conversionFee];
    }
    
    //
   if (qty.length>0) {
          _hjspkey.text = [NSString stringWithFormat:@"共%@件商品, 合计: ", qty];
    }
    CGSize size = [_hjspkey boundingRectWithSize:CGSizeMake(DEVICE_WIDTH/2, 20)];
    _hjspkey.frame =CGRectMake(PHOTO_FRAME_WIDTH, _hbzhyfkey.frame.origin.y+_hbzhyfkey.frame.size.height+PHOTO_TIME_PADDING,size.width, 20);
    _hjspvalue.frame =CGRectMake(DEVICE_WIDTH - DEVICE_WIDTH/2 -PHOTO_FRAME_WIDTH, _hbzhyfkey.frame.origin.y+_hbzhyfkey.frame.size.height+PHOTO_TIME_PADDING,DEVICE_WIDTH/2, 20);
    _hjspBtn.frame = CGRectMake(PHOTO_FRAME_WIDTH + _hjspkey.frame.origin.x + _hjspkey.frame.size.width,_hjspkey.frame.origin.y+2, 16, 16);
    NSString *totalPrice =[dict stringForKey:@"totalPrice" withDefaultValue:@""];
    if (totalPrice.length>0) {
        _hjspvalue.text =[NSString stringWithFormat:@"¥ %@", totalPrice];
    }
    
    [self layoutSubviews];
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)clickHelpEvent:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    NSString *str =[NSString stringWithFormat:@"%ld",(long)btn.tag];
    if (self.actionBlock) {
        self.actionBlock(BEventType_help,nil,self.data,str,self.indexPath);
    }
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    return PHOTO_FRAME_WIDTH*26.5;
}


@end
